package com.briup.web.servlet;

import com.briup.bean.*;
import com.briup.service.IOrderFormService;
import com.briup.service.IOrderLineService;
import com.briup.service.IShopAddressService;
import com.briup.service.impl.IOrderFormServiceImpl;
import com.briup.service.impl.IOrderLineServiceImpl;
import com.briup.service.impl.IShopAddressServiceImpl;
import org.apache.commons.lang3.StringUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.sql.Date;
import java.util.List;

/**
 * @author 王若龙
 * @CreateTime 2022-09-28 09:32
 */
@WebServlet("/orderList")
public class OrderListServlet extends HttpServlet {

    private IOrderFormService orderFormService = new IOrderFormServiceImpl();
    private IShopAddressService service = new IShopAddressServiceImpl();
    private IOrderLineService orderLineService = new IOrderLineServiceImpl();

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        HttpSession session = req.getSession();
        Customer customer = (Customer) session.getAttribute("customer");
        Integer customerId = customer.getId();

        Integer addressId = null;
        Double allPrice = (Double)session.getAttribute("allPrice");
        Date date = new Date(System.currentTimeMillis());


        String receiveName = req.getParameter("receiveName");
        String address = req.getParameter("address");
        String phone = req.getParameter("phone");
        if (!StringUtils.isAnyEmpty(receiveName, address, phone)) {
            ShopAddress shopAddress = new ShopAddress(receiveName, address, phone, new Customer(customerId));
            service.saveAddress(shopAddress);
            addressId = shopAddress.getId();
        } else {
            String shipAddId = null;
            try {
                shipAddId = req.getParameter("shipAddId");
                addressId =  Integer.parseInt(shipAddId.trim());
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        OrderForm orderForm = new OrderForm(allPrice, date, new Customer(customerId), new ShopAddress(addressId));
        orderFormService.saveOrderForm(orderForm);

       List<ShopCar> shopCars = (List<ShopCar>) session.getAttribute("shopCars");
        for (ShopCar shopCar : shopCars) {
            long num = shopCar.getNum();
            Double price = shopCar.getBook().getPrice();
            Book book = shopCar.getBook();
            OrderLine orderLine = new OrderLine(num, price * num, book, orderForm);
            orderLineService.saveOrderLine(orderLine);
        }

        List<OrderForm> orderFormList = orderFormService.findOrderFormByCustomerId(customerId);
        session.setAttribute("orderList",orderFormList);

        resp.sendRedirect("/orderlist.jsp");
    }
}
